#ifndef _BSP_IODM_H
#define _BSP_IODM_H

/**
 * \brief 求结构体成员的偏移
 * \attention 不同平台上，由于成员大小和内存对齐等原因，
 *            同一结构体成员的偏移可能是不一样的
 *
 * \par 示例
 * \code
 *  struct my_struct = {
 *      int  m1;
 *      char m2;
 *  };
 *  int offset_m2;
 *
 *  offset_m2 = AM_OFFSET(struct my_struct, m2);
 * \endcode
 */
#define AM_OFFSET(structure, member)    ((size_t)(&(((structure *)0)->member)))

/* Frame data struct Head :1 | Cmd:1 | Len:1 | Data:Len | CRC:1 | EOF:2*/

#define UART_CMD_HEAD            0x55             // 接收指令头
#define UART_CMD_RSP_HEAD        0xAA             //响应指令头


// CMD
#define IODM_DEV_RST             0x09
#define IODM_SET_BT_ADDR         0x0A
#define IODM_SET_BT_NAME         0x0B
#define IODM_CBT_TEST            0x0C
#define IODM_FCC_TEST            0x0D
#define IODM_FCC_SET_PARAM       0x0E
#define IODM_SET_XOSC_CAP        0x0F

#define IODM_GET_VER_INFO        0x19
#define IODM_GET_BT_ADDR         0x1A
#define IODM_GET_BT_NAME         0x1B
#define IODM_CBT_TEST_EXIT       0x1C
#define IODM_FCC_TEST_EXIT       0x1D
#define IODM_GET_XOSC_CAP        0x1F
#define IODM_CLEAR_ADD           0x20


//返回结果
#define IODM_RESULT_OK              0x00
#define IODM_RESULT_CHECK_FAIL      0x01
#define IODM_RESULT_CMD_UNKNOW      0x02
#define IODM_RESULT_PARAM_INVALID   0x03
#define IODM_RESULT_NO_PREPARE      0x04
#define IODM_RESULT_STAT_ERR        0x05

#define IODM_HARDWARE_VER               "\x01\x00"
#define IODM_SOFTWARE_VER               "\x01\x00"

#define RST_FLAG_MAGIC_VALUE        0x5c       //复位标志值


#define IODM_BT_VERSION                 "AB5301-V1.1-20250417"



typedef struct
{
    u8 uart1_reveice_cnt;
} iodm_test_mode;


typedef enum {
    CMD_ACK_OK = 0x00,
    CMD_ACK_ERROR = 0x01,
    CMD_SET_BT_RF_ON = 0x02,
    CMD_SET_BT_RF_OFF = 0x03,
    CMD_REPORT_CONNECT_RESULTS = 0x04,
    CMD_REPORT_DISCONNECT_RESULTS = 0x05,
    CMD_SET_BT_LOCAL_NAME = 0xa1,
    CMD_GET_BT_LOCAL_NAME = 0xa2,
    CMD_REPORT_BT_LOCAL_NAME = 0xa3,
    CMD_GET_BT_VERSION = 0xa4,
    CMD_REPORT_BT_VERSION = 0xa5,
    CMD_GET_BT_MAC_ADDR = 0xa6,
    CMD_REPORT_BT_MAC_ADDR = 0xa7,
    CMD_SET_SPP_TX_DATA = 0xc0,
    CMD_GET_SPP_RX_DATA = 0xc1,
    CMD_REPORT_SPP_RX_DATA = 0xc2,
    CMD_REPORT_SPP_CONNECT = 0xc3,
    CMD_REPORT_SPP_DISCONNECT = 0xc4,
    CMD_REPORT_SERVER_CLASS = 0xc5,
    CMD_GET_DEVICE_STATUS = 0xc6,
    CMD_REPORT_DEVICE_STATUS = 0xc7,
    CMD_REPORT_BT_LOG = 0xff,

} protocal_frame_cmd_t;

void iodm_test_mode_uart1_init(void);
void iodm_test_mode_uart0_init(void);
void iodm_reveice_data_deal(void);
void iodm_reveice_data_handle(void);
void iodm_spp_protocol_frame_response(u8 *buf, u16 len);
int iodm_protocol_frame_response(uint8_t cmd_id, uint16_t cmd_len, uint8_t *cmd_value);
void iodm_log_printf(const char *fmt, ...);
void uart1_debug_init(void);


#endif
